package com.atguigu.democustomer230201.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.democustomer230201.bean.Customer;
import com.atguigu.democustomer230201.serivce.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Consumer;

@RestController
public class CustomerController {


    @Autowired
//    @Qualifier("s1")
    CustomerService customerService;
//    @Autowired
//    @Qualifier("s2")
//    CustomerService customerService2;


    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String sayHello() {
        System.out.println("11111");
        return "hello SH";
    }
    @RequestMapping("list")
    public String getCustomer(){
        //使用Mybatis-plus方法获取表中所有的数据
        List<Customer> list = customerService.list();
        String jsonString = JSON.toJSONString(list);
        return jsonString;
    }

    @GetMapping("customer")
    public String getCustomerByNameWithAge(@RequestParam("age") Integer a, @RequestParam("name") String n) {
//        System.out.println("2222222");
        //调用Service层的方法来查询数据
//        String result = customerService.getCustomerByNameWithAge(a, n);

        /*Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("name", n).and(new Consumer<QueryWrapper<Customer>>() {
            @Override
            public void accept(QueryWrapper<Customer> customerQueryWrapper) {
                customerQueryWrapper.eq("age", a);
            }
        }));*/


        List<Customer> list = customerService.list(new QueryWrapper<Customer>().eq("name", n).eq("age", a));

        return list.toString();
    }

    @GetMapping("customerById/{id}")
    public String getCustomerById(@PathVariable("id") String n) {
        System.out.println("3333333");
//        String customer = customerService.getCustomerById(n);
        //调用Service层的方法根据id查询数据
        Customer customer = customerService.getById(n);
        return customer.toString();
    }

    @PostMapping("save")
    public String saveCustomer(@RequestBody Customer customer) {
        System.out.println(customer);
        //自己定义的方法
//        customerService.saveCustomer(customer);
        customerService.saveOrUpdate(customer);
        return "success";
    }

}
